package com.kanshu.ksgb.fastread.doudou.module.bookcity.utils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.WrapBookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.view.IRefresh;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;

/**
 * Created by wcy on 18/5/18.
 */

public class BookCityUtils {


    /**
     * {@link BookCityUtils#takeIf(String, String, Action)}
     * @param type {@link com.kanshu.ksgb.fastread.doudou.app.constants.Constants.BookCityModuleType}
     */
    public static void takeIf(ItemRefreshBean bean, String type, Action action) {
        if (bean != null) {
            takeIf(bean.type, type, action);
        }
    }

    /**
     * 检查 传入的type是否相同。 相同执行操作
     *
     * @param type1  用于判定是否执行动作的类型
     * @param action 需要执行的动作
     */
    public static void takeIf(String type1, String type2, Action action) {
        if (TextUtils.equals(type1, type2)) {
            try {
                action.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static int getIntType(String typeName) {
        int type = 0;
        switch (typeName) {
            // 精选 - 重磅推荐
            case Constants.BookCityModuleType.TYPE_SELECTED_ZHONGBANGTUIJIAN:
                type = SelectedBean.TYPE_ONE;
                break;
            // 精选 - 精选好书
            case Constants.BookCityModuleType.TYPE_SELECTED_JINGXUANHAOSHU:
                type = SelectedBean.TYPE_TWO;
                break;
            // 精选 - 完结经典
            case Constants.BookCityModuleType.TYPE_SELECTED_WANJIEJINGDIAN:
                type = SelectedBean.TYPE_THREE;
                break;
            // 精选 - 终极诱惑
            case Constants.BookCityModuleType.TYPE_SELECTED_ZHONGJIYOUHUO:
                type = SelectedBean.TYPE_FOUR_2;
                break;
            // 精选 - 私人定制
            case Constants.BookCityModuleType.TYPE_SElECTED_SIREMDINGZHI:
                type = SelectedBean.TYPE_FIVE;
                break;
            // 精选 - 专题
//            case Constants.BookCityModuleType.TYPE_SELECTED_ZHUANTI:
//                type = SelectedBean.TYPE_FIVE;
//                break;
            // 精选 - 本周强推
            case Constants.BookCityModuleType.TYPE_SELECTED_BENZHOUQIANGTUI:
                type = SelectedBean.TYPE_ONE;
                break;
            // 精选 - 人气专区
            case Constants.BookCityModuleType.TYPE_SELECTED_RENQIZHUANQU:
                type = SelectedBean.TYPE_EIGHT;
                break;
            // 精选 - 新书抢先
            case Constants.BookCityModuleType.TYPE_SELECTED_XINSHUQIANGXIAN:
                type = SelectedBean.TYPE_FOUR_1;
                break;
            // 精选 - 全站畅读
            case Constants.BookCityModuleType.TYPE_SELECTED_QUANZHANCHANGDU:
                type = SelectedBean.TYPE_SIX;
                break;
            // 精选 - 热门标签
            case Constants.BookCityModuleType.TYPE_SElECTED_REMENBIAOQIAN:
                type = SelectedBean.TYPE_SEVEN;
                break;
            // 男频 - 本周热搜
            case Constants.BookCityModuleType.TYPE_MALE_BENZHOURESOU:
                type = SelectedBean.TYPE_FOUR_3;
                break;
            // 男频 - 男频经典
            case Constants.BookCityModuleType.TYPE_MALE_NANPINJINGDIAN:
                type = SelectedBean.TYPE_ONE;
                break;
            // 男频 - 完结精品
            case Constants.BookCityModuleType.TYPE_MALE_WANJIEJINGDIAN:
                type = SelectedBean.TYPE_TWO;
                break;
            // 男频 - 人气专区
            case Constants.BookCityModuleType.TYPE_MALE_RENQIZHUANQU:
                type = SelectedBean.TYPE_FOUR_4;
                break;
            // 男频 - 热门标签
            case Constants.BookCityModuleType.TYPE_MALE_REMENBIAOQIAN:
                type = SelectedBean.TYPE_SEVEN;
                break;
            // 男频 - 私人订制
            case Constants.BookCityModuleType.TYPE_MALE_SIRENDINGZHI:
                type = SelectedBean.TYPE_ONE;
                break;
            // 男频 - 都市生活
            case Constants.BookCityModuleType.TYPE_MALE_DUSHISHENGHUO:
                type = SelectedBean.TYPE_TWO;
                break;
            // 男频 - 玄幻科幻
            case Constants.BookCityModuleType.TYPE_MALE_XUANHUANKEHUAN:
                type = SelectedBean.TYPE_EIGHT;
                break;
            // 男频 - 热门分类
            case Constants.BookCityModuleType.TYPE_MALE_REMENFENLEI:
                type = SelectedBean.TYPE_FOUR_5;
                break;
            // 男频 - 换分类1
            case Constants.BookCityModuleType.TYPE_MALE_HUANFENLEI1:
                type = SelectedBean.TYPE_TWO;
                break;
            // 男频 - 换分类2
            case Constants.BookCityModuleType.TYPE_MALE_HUANFENLEI2:
                type = SelectedBean.TYPE_EIGHT;
                break;
            case Constants.BookCityModuleType.TYPE_MALE_HUANFENLEI3:
                type = SelectedBean.TYPE_FOUR_6;
                break;
            // 女频 - 本周热搜
            case Constants.BookCityModuleType.TYPE_FEMALE_BENZHOURESOU:
                type = SelectedBean.TYPE_FOUR_3;
                break;
            // 女频 - 力荐
            case Constants.BookCityModuleType.TYPE_FEMALE_LIJIAN:
                type = SelectedBean.TYPE_ONE;
                break;
            // 女频 - 全本
            case Constants.BookCityModuleType.TYPE_FEMALE_QUANBEN:
                type = SelectedBean.TYPE_TWO;
                break;
            // 女频 - 经典
            case Constants.BookCityModuleType.TYPE_FEMALE_JINGDIAN:
                type = SelectedBean.TYPE_ONE;
                break;
            // 女频 - 热门标签
            case Constants.BookCityModuleType.TYPE_FEMALE_REMENBIAOQIAN:
                type = SelectedBean.TYPE_SEVEN;
                break;
            // 女频 - 宫闱情仇
            case Constants.BookCityModuleType.TYPE_FEMALE_GONGWEIQINGCHOU:
                type = SelectedBean.TYPE_TWO;
                break;
            // 女频 - 总裁豪门
            case Constants.BookCityModuleType.TYPE_FEMALE_ZONGCAIHAOMEN:
                type = SelectedBean.TYPE_EIGHT;
                break;
            // 女频 - 架空历史
            case Constants.BookCityModuleType.TYPE_FEMALE_JIAKONGLISHI:
                type = SelectedBean.TYPE_SIX;
                break;
            // 女频 - 人气
            case Constants.BookCityModuleType.TYPE_FEMALE_RENQI:
                type = SelectedBean.TYPE_FOUR_4;
                break;
            // 女频 - 热门分类
            case Constants.BookCityModuleType.TYPE_FEMALE_REMENFENLEI:
                type = SelectedBean.TYPE_FOUR_5;
                break;
            // 女频 - 换分类1
            case Constants.BookCityModuleType.TYPE_FEMALE_HUANFENLEI1:
                type = SelectedBean.TYPE_TWO;
                break;
            // 女频 - 换分类2
            case Constants.BookCityModuleType.TYPE_FEMALE_HUANFENLEI2:
                type = SelectedBean.TYPE_EIGHT;
                break;
            // 女频 - 换分类3
            case Constants.BookCityModuleType.TYPE_FEMALE_HUANFENLEI3:
                type = SelectedBean.TYPE_SIX;
                break;
        }
        return type;
    }

    public static ItemRefreshBean getItemRefreshBean(int type, int pos, List<SelectedBean> items, RecyclerView.Adapter adapter) {
        if (Utils.isEmptyList(items)) {
            return null;
        }
        ItemRefreshBean bean = new ItemRefreshBean();
        SelectedBean item = items.get(pos);
        bean.adapter = adapter;
        bean.pos = pos;
        bean.type = item.type;
        bean.selectionName = item.name;
        bean.subTitle = item.sub_name;
        bean.selectedItem = item;
        bean.list = items;
        bean.tabType = type;
        if (type == Constants.BookCityTabType.TYPE_SELECTED) {
            bean.selectionIcon = R.mipmap.ic_selected_indicator;
        } else if (type == Constants.BookCityTabType.TYPE_MALE) {
            bean.selectionIcon = R.mipmap.ic_male_indicator;
        } else if (type == Constants.BookCityTabType.TYPE_FEMALE) {
            bean.selectionIcon = R.mipmap.ic_female_indicator;
        }
        return bean;
    }

    public static int updateItemPage(ItemRefreshBean item) {
        if (item.selectedItem.page_info == null) {
            return 1;
        }
        int page = Integer.parseInt(item.selectedItem.page_info.cur_page);
        if (page >= item.selectedItem.page_info.total_page) {
            item.selectedItem.page_info.cur_page = "1";
            return 1;
        }
        item.selectedItem.page_info.cur_page = String.valueOf(++page);
        return page;
    }

    public static void refreshModuleInfos(ItemRefreshBean item, final IRefresh<SelectedBean> refresh, BookCityPresenter presenter) {
        refreshModuleInfos(item, refresh, null, presenter);
    }

    public static void refreshModuleInfos(ItemRefreshBean item, final IRefresh<SelectedBean> refresh, INetCommCallback<SelectedBean> callback, BookCityPresenter presenter) {
        if (item == null) {
            return;
        }
        presenter.getSelectedItem(item, new INetCommCallback<SelectedBean>() {
            @Override
            public void onResponse(SelectedBean selectedBean) {
                item.selectedItem = selectedBean;
                item.list.set(item.pos, selectedBean);
                if (callback != null) {
                    callback.onResponse(selectedBean);
                } else {
                    refresh.refresh(selectedBean, item);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                if (callback != null) {
                    callback.onError(code, errDesc);
                } else {
                    refresh.onError(code, errDesc);
                }
            }
        });
    }

    public static void switchMaleCategory(ItemRefreshBean item, final IRefresh<SelectedBean> refresh, INetCommCallback<SelectedBean> callback, BookCityPresenter presenter) {
        if (item == null) {
            return;
        }
        presenter.getMaleSwitchCategory(new INetCommCallback<SelectedBean>() {
            @Override
            public void onResponse(SelectedBean selectedBean) {
                selectedBean.type = item.type;
                item.selectedItem = selectedBean;
                item.list.set(item.pos, selectedBean);
                if (callback != null) {
                    callback.onResponse(selectedBean);
                } else {
                    refresh.refresh(selectedBean, item);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                if (callback != null) {
                    callback.onError(code, errDesc);
                } else {
                    refresh.onError(code, errDesc);
                }
            }
        });
    }

    public static void switchFemaleCategory(ItemRefreshBean item, final IRefresh<SelectedBean> refresh, INetCommCallback<SelectedBean> callback, BookCityPresenter presenter) {
        if (item == null) {
            return;
        }
        presenter.getFemaleSwitchCategory(new INetCommCallback<SelectedBean>() {
            @Override
            public void onResponse(SelectedBean selectedBean) {
                selectedBean.type = item.type;
                item.selectedItem = selectedBean;
                item.list.set(item.pos, selectedBean);
                if (callback != null) {
                    callback.onResponse(selectedBean);
                } else {
                    refresh.refresh(selectedBean, item);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                if (callback != null) {
                    callback.onError(code, errDesc);
                } else {
                    refresh.onError(code, errDesc);
                }
            }
        });
    }


    public static void getHotLabel(ItemRefreshBean item, final IRefresh<SelectedBean> refresh, INetCommCallback<SelectedBean> callback, BookCityPresenter presenter) {
        if (item == null) {
            return;
        }
        presenter.getHotLabel(item, new INetCommCallback<SelectedBean>() {
            @Override
            public void onResponse(SelectedBean selectedBean) {
                item.selectedItem = selectedBean;
                item.list.set(item.pos, selectedBean);
                if (callback != null) {
                    callback.onResponse(selectedBean);
                } else {
                    refresh.refresh(selectedBean, item);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }


    /**
     * 构建新的数据结构
     *
     * @param page
     * @param list
     * @param limit
     * @return
     */
    public static List<WrapBookInfo> WrapBookInfo(int page, List<BookInfo> list, int limit, int totalPage) {
        return WrapBookInfo(page, null, list, limit, totalPage);
    }

    public static List<WrapBookInfo> WrapBookInfo(int page, List<WrapBookInfo> originList, List<BookInfo> list, int limit, int totalPage) {
        List<WrapBookInfo> list1 = new ArrayList<>();
        if (page == 1 || Utils.isEmptyList(originList)) {
            WrapBookInfo bookInfo = new WrapBookInfo();
            bookInfo.totalPage = totalPage;
            bookInfo.type = WrapBookInfo.TYPE_ONE;
            bookInfo.items = new ArrayList<>();
            limit = limit > list.size() ? list.size() : limit;
            for (int i = 0; i < limit; i++) {
                bookInfo.items.add(list.get(i));
            }
            list1.add(bookInfo);
            for (int i = limit; i < list.size(); i++) {
                WrapBookInfo bookInfo1 = new WrapBookInfo();
                bookInfo1.type = WrapBookInfo.TYPE_TWO;
                bookInfo1.item = list.get(i);
                list1.add(bookInfo1);
            }
        } else {
            for (int i = 0; i < list.size(); i++) {
                WrapBookInfo bookInfo1 = new WrapBookInfo();
                bookInfo1.type = WrapBookInfo.TYPE_TWO;
                bookInfo1.item = list.get(i);
                bookInfo1.totalPage = totalPage;
                list1.add(bookInfo1);
            }
        }
        return list1;
    }

    public static View addEmptyFooter(Context context, int paddingTop, int paddingBottom) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_empty_footer_layout, null);
        if (paddingBottom == 0 && paddingTop == 0) {
            return view;
        }
        View v = view.findViewById(R.id.root_footer);
        v.setPadding(0, paddingTop, 0, paddingBottom);
        return view;
    }

    public static void showHighLightNum(TextView tv, String s1, int num) {
        SpannableString cs = new SpannableString(s1);
        String s = String.valueOf(num);
        int index = s1.indexOf(s);
        if (index > -1) {
            cs.setSpan(new ForegroundColorSpan(tv.getContext().getResources().getColor(R.color.theme)), index, index + s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        tv.setText(cs);
    }

    public static void rotate(View change) {
        if (change == null) {
            return;
        }
        change.clearAnimation();
        Animation rotate = AnimationUtils.loadAnimation(change.getContext(), R.anim.anim_selected_swtich);
        change.startAnimation(rotate);
    }

    public static void rotateCancel(View change) {
        if (change == null) {
            return;
        }
        AndroidSchedulers.mainThread().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                Animation animation = change.getAnimation();
                if (animation != null) {
                    animation.cancel();
                    change.clearAnimation();
                }
            }
        }, 550, TimeUnit.MILLISECONDS);
    }

}
